﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
    public class IPProveedor : Intermediario
    {
        public override List<T> getAll<T>()
        {
            return (from pr in dao.Proveedors orderby pr.IdProveedor select pr).ToList() as List<T>;
        }

        public override List<T> getByCriterio<T>(string att, string op, string val)
        {
            List<T> listita = dao.ExecuteQuery<Proveedor>("SELECT * FROM Proveedor WHERE Proveedor." + att + " " + op + " '" + val + "' AND Proveedor.fechaBaja IS NULL ORDER BY Proveedor.RazonSocial").ToList() as List<T>;

            return listita;
        }

        public override void save<T>(T entity)
        {
            try
            {
                Proveedor pr = entity as Proveedor;
               // pr.Activo = true;
                dao.Proveedors.InsertOnSubmit(pr);
            }
            catch (Exception ex)
            {
                throw new Exception("IPRProveedor.save - " + ex.Message);
            }
        }

        public void update<T>(T entity)
        {
            try
            {
                Proveedor pr = entity as Proveedor;
                dao.Proveedors.InsertOnSubmit(pr);
            }
            catch (Exception ex)
            {
                throw new Exception("IPRProveedor.update - " + ex.Message);
            }
        }

        public override IList<T> getCriterioById<T>(string att, string op, int val)
        {
            throw new NotImplementedException();
        }

        public override object getPorId(int id)
        {
            throw new NotImplementedException();
        }

        public override IList<T> getPorCriterio<T>(string name)
        {
            throw new NotImplementedException();
        }

        public override void Update<T>(T entity)
        {
            try
            {
                Proveedor pr = entity as Proveedor;
                dao.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("IPRProveedor.Update - " + ex.Message);
            }
        }

        public override void Delete(int valor)
        {
            string dt = DateTime.Now.Date.ToShortDateString();
            dao.ExecuteQuery<Proveedor>("UPDATE Proveedor SET fechaBaja = " + dt + " " + "WHERE Id = " + valor);
        }
    }
}
